package org.galagosearch.core.index;

import java.io.IOException;
import java.io.OutputStream;

 public class VersionedPostingList implements IndexElement {
        public VersionedPostingList() {
            documents = new BackedCompressedByteBuffer();
            counts = new BackedCompressedByteBuffer();
            tf = new BackedCompressedByteBuffer();
            te = new BackedCompressedByteBuffer();
            tb = new BackedCompressedByteBuffer();
            header = new BackedCompressedByteBuffer();
        }

        public void close() throws IOException {
            int options = 0;

            if (documents.length() > 0) {
                counts.add(positionCount);
            }
            header.add(options);

            header.add(documentCount);
            header.add(totalPositionCount);
            
            header.add(documents.length());
            header.add(counts.length());
            header.add(tf.length());
            header.add(tb.length());
            header.add(te.length());
            
            System.out.println(options+"\t"+documentCount+"\t"+totalPositionCount+"\t"+documents.length()+"\t"+tf.length()+"\t"+tb.length()+"\t"+te.length());
        }

        public long dataLength() {
            long listLength = 0;

            listLength += header.length();
            listLength += counts.length();
            listLength += tf.length();
            listLength += documents.length();
            listLength += tb.length();
            listLength += te.length();

            return listLength;
        }

        public void write(final OutputStream output) throws IOException {
            header.write(output);
            header.clear();

            documents.write(output);
            documents.clear();

            counts.write(output);
            counts.clear();

            tf.write(output);
            tf.clear();
            
            tb.write(output);
            tb.clear();
            
            te.write(output);
            te.clear();
        }

        public byte[] key() {
            return word;
        }

        public void setWord(byte[] word) {
            this.word = word;
            this.lastDocument = 0;
            this.lastTermFreq = 0;
            this.totalPositionCount = 0;
            this.lastTB = 0;
            this.lastTE = 0;
            this.positionCount = 0;
        }

        public void addDocument(long documentID) throws IOException {
            // add the last document's counts
            if (documents.length() > 0) {
                counts.add(positionCount);
            }
            documents.add(documentID - lastDocument);
            lastDocument = documentID;

            lastTermFreq = 0;
            positionCount = 0;
            documentCount++;
        }

        public void addTF(int curTF) throws IOException {
            positionCount++;
            totalPositionCount++;
            tf.add(curTF - lastTermFreq);
            lastTermFreq = curTF;
        }
        public void addTB(int curTB) throws IOException {
        //    positionCount++;
        //    totalPositionCount++;
            tb.add(curTB - lastTB);
            lastTB = curTB;
        }
        public void addTE(int curTE) throws IOException {
        //    positionCount++;
        //    totalPositionCount++;
        	positionCount=3;
        	totalPositionCount+=3;
            te.add(curTE - lastTE);
            lastTE = curTE;
        }
        private long lastDocument;
        private int lastTB;
        private int lastTE;
        private int lastTermFreq;
        private int positionCount;
        private int documentCount;
        private int totalPositionCount;
        public byte[] word;
        public BackedCompressedByteBuffer header;
        public BackedCompressedByteBuffer documents;
        public BackedCompressedByteBuffer counts;
        public BackedCompressedByteBuffer tf;
        public BackedCompressedByteBuffer tb;
        public BackedCompressedByteBuffer te;
}

